function mergeSort(nums = []){
    let len = nums.length
    if(len < 2){
        return nums
    }
    // 合并两个已经排好序的数组
    function merge(arr1 = [], arr2 = [], left){
        while (arr1.length && arr2.length) {
            nums[left++] = arr1[0] <= arr2[0] ? arr1.shift() : arr2.shift()
        }
        while(arr1.length){
            nums[left++] = arr1.shift()
        }
        while(arr2.length){
            nums[left++] = arr2.shift()
        }
    }

    function loop(left, right){
        if(left < right){
            let mid = Math.floor((left + right) / 2)
            loop(left, mid)
            loop(mid+1, right)
            let arr1 = JSON.parse(JSON.stringify(nums.slice(left, mid+1)))
            let arr2 = JSON.parse(JSON.stringify(nums.slice(mid+1, right+1)))
            merge(arr1, arr2, left)
            console.log(nums)
        }
    }
    loop(0, len - 1)
}



const nums = [2,1,6,7,1,8,3,5,4,7]
mergeSort(nums)